﻿Imports System.ComponentModel

Class Application


	' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
	' can be handled in this file.

	''' <summary>
	''' This is a helper that retrieves the show file stored in the resources section of the application.
	''' </summary>
	''' <value></value>
	''' <returns></returns>
	''' <remarks></remarks>
	Public ReadOnly Property Show As Shows.ShowContainer
		Get
			Return Global.System.Windows.Application.Current.Resources("ShowFile")
		End Get
	End Property


	''' <summary>
	''' Handles checking to see if a file is being passed to the application to open automatically.
	''' </summary>
	''' <param name="sender"></param>
	''' <param name="e"></param>
	''' <remarks></remarks>
	Private Async Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
		''If an instance of this application is already running, activate that and quit this.
		'Try
		'	Dim processList() As Process
		'	processList = Process.GetProcessesByName("Q2Q")
		'	Dim proc = Process.GetCurrentProcess
		'	'MsgBox("Current Handles: " & vbCrLf & proc.Handle.ToString & vbCrLf & proc.MainWindowHandle.ToString)
		'	If processList.Count > 0 Then
		'		'AppActivate(processList(0).Id)
		'		If processList(0).Id <> Process.GetCurrentProcess.Id Then
		'			'MsgBox("Existing Handles: " & vbCrLf & processList(0).Handle.ToString & vbCrLf & processList(0).MainWindowHandle.ToString)
		'			Win32.ShowWindow(processList(0).MainWindowHandle, Win32.ShowWindowCommands.SW_SHOW)
		'			Me.Shutdown()
		'		End If
		'	End If
		'Catch
		'End Try

		'Before the window is visible set themes.
		If My.Settings.Theme = "Dark" Then
			Dim rd As New ResourceDictionary()
			rd.Source = New Uri("/Styles/Theme.Dark.xaml", UriKind.Relative)
			My.Application.Resources.MergedDictionaries.Insert(1, rd)
			My.Application.Resources.MergedDictionaries.RemoveAt(2)
		End If


		'Before the application gets going we have to create a new show container and add it to the program's resources.
		'This allows us to quickly access the show information through: My.Application.Show
		Dim Container As New Shows.ShowContainer()
		My.Application.Resources.Add("ShowFile", Container)
		My.Application.Show.Console.SendMessage("Application Started")

		If e.Args.Length > 0 Then
			Try
                If FileIO.FileSystem.FileExists(e.Args(0)) Then
                    My.Application.Show.ShowFile = Await Shows.ShowFile.Load(e.Args(0))
                End If
            Catch ex As Exception
			End Try
		End If

	End Sub

End Class
